package cn.edu.heuet.bnve.activity.ScheduleActivity;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;

import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import cn.edu.heuet.bnve.R;


import java.io.IOException;

import cn.edu.heuet.bnve.databinding.ActivitySetAlarmToneBinding;

/**
 * Created by abc-pc on 2020/4/18.
 */
public class SetAlarmToneActivity extends AppCompatActivity {

    ActivitySetAlarmToneBinding binding;

    RecyclerView tone_recycler;
    private String toneName;
    private String tonePath;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_set_alarm_tone);

        tone_recycler = binding.toneRecycler;
        tone_recycler.setLayoutManager(new LinearLayoutManager(this));
        SetAlarmToneAdapter adapter = new SetAlarmToneAdapter(this);
        tone_recycler.setAdapter(adapter);

        adapter.setOnItemClickListener(new SetAlarmToneAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, String data,int position) {
                Toast.makeText(SetAlarmToneActivity.this, data + "-" + position, Toast.LENGTH_SHORT).show();
                toneName = data;
                RingtoneManager rm = new RingtoneManager(SetAlarmToneActivity.this);
                rm.setType(RingtoneManager.TYPE_RINGTONE);
                rm.getCursor();
                Uri ringtoneUri = rm.getRingtoneUri(position);
                tonePath = ringtoneUri.toString();
                if (mediaPlayer == null) {
                    mediaPlayer = new MediaPlayer();
                } else {
                    if (mediaPlayer.isPlaying())
                        mediaPlayer.stop();
                    mediaPlayer.reset();
                }
                try {
                    mediaPlayer.setDataSource(SetAlarmToneActivity.this, ringtoneUri);
                    mediaPlayer.setVolume(1f, 1f);
                    mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                    mediaPlayer.setLooping(false);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.txSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.putExtra("sound", toneName);
                intent.putExtra("soundPath",tonePath);
                setResult(2, intent);
                finish();
            }
        });
    }

    private void initView(){

    }

    public static void show(Context context) {
        context.startActivity(new Intent(context, SetAlarmToneActivity.class));
    }

    @Override
    public void finish() {
        super.finish();
        if(mediaPlayer!=null){
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }

    }
}
